﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StartupTaskRunner.Configuration;

namespace StartupTaskRunner.Actions
{
    public class ActionFactory
    {
        internal static IAction GetAction(ActionType actionType, ConfigType configuration)
        {
            IAction action = null;

            if (actionType is CopyType) 
            {
                action = CreateCopyAction(actionType as CopyType);
            }
            else if (actionType is RunType)
            {
                action = CreateRunAction(actionType as RunType);
            }
            else if (actionType is InstallCertificateType)
            {
                action = CreateInstallCertificateAction(actionType as InstallCertificateType);
            }
            else if (actionType is UnZipType)
            {
                action = CreateUnzipAction(actionType as UnZipType);
            }
            action.Configuration = configuration;

            return action;
        }

        private static IAction CreateUnzipAction(UnZipType unZipType)
        {
            UnZipAction action = new UnZipAction();

            action.ContainerName = unZipType.containerName;
            action.ArchiveName = unZipType.archiveName;
            action.DestinationLocation = unZipType.destinationLocation;
            action.Overwrite = unZipType.overwrite;

            return action;
        }

        private static IAction CreateRunAction(RunType runType)
        {
            RunAction action = new RunAction();

            action.CommandFile = runType.commandFile;
            action.CommandArguments = runType.commandArguments;
            action.TimeOut = runType.timeout;

            return action;
        }

        private static IAction CreateCopyAction(CopyType copyType)
        {
            CopyAction action = new CopyAction();

            action.ContainerName = copyType.containerName;
            action.DestinationLocation = copyType.destinationLocation;
            action.Overwrite = copyType.overwrite;
            action.SourceFile = copyType.sourceFile;

            return action;
        }

        private static IAction CreateInstallCertificateAction(InstallCertificateType installCertType)
        {
            InstallCertificateAction action = new InstallCertificateAction();

            action.CertificateFile = installCertType.certificateFile;
            action.CertStoreLocation = installCertType.certificateStoreLocation.ToString();
            action.CertStoreName = installCertType.certificateStoreName;

            return action;
        }

        internal static List<IAction> GetActions(ActionType[] actionTypes, ConfigType configuration)
        {
            List<IAction> actions = new List<IAction>();

            foreach (var actionType in actionTypes)
            {
                actions.Add(GetAction(actionType, configuration));
            }

            return actions;
        }
    }
}
